package com.edu.dao.impl;

import com.edu.bean.User;
import com.edu.dao.BaseDao;
import com.edu.dao.UserDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {

    @Override
    public User selectUserNameAndPassWord(String userName, String passWord) {
        User user = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        String sql = "SELECT * FROM user WHERE userName = ? and passWord = ?";
        try {
            con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1, userName);
            ps.setString(2, passWord);
            res = ps.executeQuery();
            if(res.next()) {
                user = new User();
                user.setUserId(res.getInt("userId"));
                user.setUserName(res.getString("userName"));
                user.setPassWord(res.getString("passWord"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.closeAll( res,ps,con);
        }
        return user;
    }

    @Override
    public User selectUserName(String userName) {
        User user = null;
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet res = null;
        String sql = "SELECT * FROM user WHERE userName = ?";
        try {
            con = BaseDao.getCon();
            ps = con.prepareStatement(sql);
            ps.setString(1, userName);
            res = ps.executeQuery();
            if(res.next()) {
                user = new User();
                user.setUserId(res.getInt("userId"));
                user.setUserName(res.getString("userName"));
                user.setPassWord(res.getString("passWord"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            BaseDao.closeAll( res,ps,con);
        }
        return user;
    }
}
